﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Dack_Web2
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //lưu trang chuyển đến lại
                if (Request.UrlReferrer != null)
                    ViewState["RefUrl"] = Request.UrlReferrer.ToString();
                if (CurrentContext.IsLogged)
                {
                    CurrentContext.User = Dack_Web2.User.FromId(CurrentContext.UserName);
                    Response.Redirect("~/Default.aspx");
                }
            }
        }

       

        protected void btnDangNhap_Click(object sender, EventArgs e)
        {
            if (txtTenDangNhap.Text == "" || txtMatKhau.Text == "")
            {

            }
            else
            {
                User Us = new User(txtTenDangNhap.Text, txtMatKhau.Text);
                if (Us.IsValidLogin())
                {
                    Response.Cookies["TenDangNhap"].Value = Us.TenDN;
                    if (chkRemember.Checked)
                    {
                        Response.Cookies["TenDangNhap"].Expires = DateTime.Now.AddDays(20);//Gia han thoi gian dang nhap
                    }
                    
                    Us.InitCart();
                    CurrentContext.User = Us;
                  
                    if (txtTenDangNhap.Text.ToUpper() == "ADMIN")
                    {
                        //Quăng qua trang admin ----------------------
                        Response.Redirect("~/MainAdmin.aspx");
                        //end 
                    }
                    else
                    {
                        string url = ViewState["RefUrl"] == null ? "~/Default.aspx" :
                        ViewState["RefUrl"].ToString();
                        Response.Redirect(url);
                    }
                   
                }
            }
        }
    }
}